// 模块管理
export const moduleService = {
  getModules() {
    const modules = localStorage.getItem('project_modules')
    return modules ? JSON.parse(modules) : []
  },
  saveModule(module) {
    const modules = this.getModules()
    const index = modules.findIndex(m => m.id === module.id)
    if (index > -1) {
      modules[index] = module
    } else {
      module.id = Date.now().toString()
      modules.push(module)
    }
    localStorage.setItem('project_modules', JSON.stringify(modules))
    return module
  },
  deleteModule(id) {
    const modules = this.getModules()
    const newModules = modules.filter(m => m.id !== id)
    localStorage.setItem('project_modules', JSON.stringify(newModules))
  }
}

// 需求管理
export const requirementService = {
  getRequirements() {
    const requirements = localStorage.getItem('project_requirements')
    return requirements ? JSON.parse(requirements) : []
  },
  saveRequirement(requirement) {
    const requirements = this.getRequirements()
    const index = requirements.findIndex(r => r.id === requirement.id)
    if (index > -1) {
      requirements[index] = requirement
    } else {
      requirement.id = Date.now().toString()
      requirements.push(requirement)
    }
    localStorage.setItem('project_requirements', JSON.stringify(requirements))
    return requirement
  },
  deleteRequirement(id) {
    const requirements = this.getRequirements()
    const newRequirements = requirements.filter(r => r.id !== id)
    localStorage.setItem('project_requirements', JSON.stringify(newRequirements))
  },
  getRequirementStats() {
    const requirements = this.getRequirements()
    const stats = {
      '待处理': { count: 0, total: 0 },
      '进行中': { count: 0, total: 0 },
      '已完成': { count: 0, total: 0 },
      '已拒绝': { count: 0, total: 0 }
    }
    
    // 计算各状态的需求数量
    requirements.forEach(req => {
      stats[req.status].count++
      stats[req.status].total++
    })
    
    // 计算总需求数
    const total = requirements.length
    
    // 转换为数组格式并计算百分比
    return Object.entries(stats).map(([status, data]) => ({
      status,
      count: data.count,
      percentage: total ? Math.round((data.count / total) * 100) : 0,
      total: total
    }))
  }
} 